/* EasyWay Game Engine
 * Copyright (C) 2007 Daniele Paggi.
 *  
 * Written by: 2007 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.editor.commands.user;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.easyway.editor.commands.Command;
import org.easyway.editor.forms.EditorCore;
import org.easyway.gui.base.Button;
import org.easyway.interfaces.base.ITexture;
import org.easyway.objects.text.EWText;
import org.easyway.system.StaticRef;

public class GetTexture extends Command {

	static Condition getPointCondition;

	static Lock lock;

	public static ITexture getTexture() {
		boolean oldState = EditorCore.showLayer[3];
		boolean oldState6 = EditorCore.showLayer[6];
		EditorCore.showLayer[3] = EditorCore.showLayer[6] = true;
		System.out.println("command: select a texture");
		lock = new ReentrantLock();
		getPointCondition = lock.newCondition();
		GetTexture gp = new GetTexture();
		try {
			lock.lock();
			getPointCondition.await();
			lock.unlock();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(" Texture selected      ["
				+ gp.texture.toString() + "]");
		EditorCore.showLayer[3] = oldState;
		EditorCore.showLayer[6] = oldState6;
		return gp.texture;
	}

	boolean selected;

	ITexture texture;

	Button[] buttons;

	int page = 0;
	
	Button prev, next;

	public GetTexture() {
		selected = false;
	}

	public void creation() {
		prev = new Button(0, 0, EWText.createColor(255, 0, 0) + "prev") {

			private static final long serialVersionUID = 1L;

			@Override
			public void command() {
				prevPage();
			}
		};
		next = new Button(prev.getWidth(), 0, EWText.createColor(255, 0, 0) + "next") {
			private static final long serialVersionUID = 1L;

			@Override
			public void command() {
				nextPage();
			}
		};
		buttons = new Button[9];
		for (int y = 0; y < 3; ++y) {
			for (int x = 0; x < 3; ++x) {
				(buttons[x + y * 3] = new Button(120 + x * 200, y * 200, null) {

					private static final long serialVersionUID = 1L;

					public void command() {
						texture = getImage();
						if (texture!=null)
						selected = true;
					}
				}).setSize(200, 200);
			}
		}
		page = 0;
		setPage();

	}

	public void setPage() {
		if (page < 0)
			page = (int) StaticRef.textures.size() / 9;
		if ((page == (int) StaticRef.textures.size() / 9 && StaticRef.textures
				.size() % 9 == 0)
				|| page > (int) StaticRef.textures.size() / 9) {
			page = 0;
		}
		int tpage = page * 9;
		int c = 0;
		for (int i = tpage; i < tpage + 9; ++i) {
			if (i >= StaticRef.textures.size())
				buttons[c].setUpImage(null);
			else
				buttons[c].setUpImage(StaticRef.textures.get(i));
			c++;
		}
	}

	public void prevPage() {
		--page;
		setPage();
	}

	public void nextPage() {
		++page;
		setPage();
	}

	public void loop() {
		if (selected) {
			lock.lock();
			getPointCondition.signal();
			lock.unlock();
			destroy();
		}
	}

	public void render() {
	}

	public void destroy() {
		super.destroy();
		System.out.println("DESTROY");
		for (int i = buttons.length - 1; i >= 0; i--) {
			if (buttons[i] != null && !buttons[i].isDestroyed())
				buttons[i].destroy();
		}
		prev.destroy();
		next.destroy();
	}

}
